package Diffenent;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class FindDuplicateDemo {

	public static void main(String[] args) {
		long begin = System.currentTimeMillis();
		Map<String, ArrayList<String>> DuplicateValues =	readFileAll_Info("C:\\a.txt");
		prt(DuplicateValues);
		long end = System.currentTimeMillis();
		System.out.println(end-begin);
	}

	public static Map<String, ArrayList<String>> readFileAll_Info(String fileName) {
		File file = new File(fileName);
		Map<String, ArrayList<String>> mapAll = new HashMap<String, ArrayList<String>>();
		Map<String, ArrayList<String>> MapDuplicate = new HashMap<String, ArrayList<String>>();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			while ((tempString = reader.readLine()) != null) {
				// System.out.println(tempString+"  行"+line);
				ArrayList<String> temp = mapAll.get(tempString);
				if (temp != null) {
					String currentLine = tempString + "   行" + line;
					temp.add(currentLine);
					mapAll.put(tempString, temp);
					MapDuplicate.put(tempString, temp);
				} else {
					temp = new ArrayList<String>();
					String currentLine = tempString + "   行" + line;
					temp.add(currentLine);
					mapAll.put(tempString, temp);
				}
				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return MapDuplicate;
	}
	
	public static void prt(Map<String, ArrayList<String>> map) {
		Iterator it1 = null;
		Map.Entry entry = null;
		it1 = map.entrySet().iterator();
		while (it1.hasNext()) {
			entry = (Map.Entry) it1.next();
			//System.out.println("key: " + entry.getKey());
			ArrayList list = (ArrayList) entry.getValue();
			int len = list.size();
			for (int i = 0; i < len; i++) {
				System.out.println("重复的值  = "+list.get(i));
			}
		}
	}
	
}
